﻿using FlexFlow.Base.Interface;
using FlexFlow.Model;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.DataProvider.MongoDB
{
    public class MongoDbWorkFlowTaskProvider : MongoDBPhysicalModelProvider<WorkFlowTask>,IWorkFlowTaskProvider
    {
        public override WorkFlowTask Create()
        {
            return new WorkFlowTask()
            {
                SendTime = DateTime.Now,
                Reciver = String.Empty
            };
        }
        /// <summary>
        /// 取得指定用户发送的任务列表
        /// </summary>
        /// <param name="sender">发送者</param>
        /// <returns>
        /// 任务列表
        /// </returns>
        public List<WorkFlowTask> GetTaskBySender(Guid sender)
        {
            FilterDefinition<WorkFlowTask> filter = (FilterDefinition<WorkFlowTask>)(m => m.Sender == sender);
            return GetCollection().GetAll(filter);
        }
        /// <summary>
        /// 取得发送给指定用户的任务列表
        /// </summary>
        /// <param name="reciver">任务的接收者</param>
        /// <returns>
        /// 任务列表
        /// </returns>
        public List<WorkFlowTask> GetTaskByReciver(Guid reciver)
        {
            BsonRegularExpression exp = new BsonRegularExpression(reciver.ToString());
            FilterDefinition<WorkFlowTask> filter = Builders<WorkFlowTask>.Filter.Regex(m => m.Reciver, exp);
            return GetCollection().GetAll(filter);
        }
        /// <summary>
        /// 更新指定任务的状态以及消息
        /// </summary>
        /// <param name="identity">
        /// 任务ID
        /// </param>
        /// <param name="state">
        /// 任务状态
        /// </param>
        /// <param name="message">
        /// 任务消息
        /// </param>
        /// <returns>
        /// 一个值，表示操作是否成功
        /// </returns>
        public bool UpdateState(Guid identity, WorkFlowTaskState state, string message = "")
        {
            var updef = BuildUpdateDef(m => m.State, state);
            updef = BuildUpdateDef(m => m.Message, message, updef);
            return Update(identity, updef);
        }
        /// <summary>
        /// 更新指定任务的提交数据
        /// </summary>
        /// <param name="identity">
        /// 任务Id
        /// </param>
        /// <param name="data">
        /// 提交数据
        /// </param>
        /// <returns>
        /// 操作结果
        /// </returns>
        public bool UpdateSubmitData(Guid identity, Dictionary<string, object> data)
        {
            var updef = BuildUpdateDef(m => m.SubmitData, data);
            return Update(identity, updef);
        }

        /// <summary>
        /// 更新指定任务的状态为已打开并且更新打开人员
        /// </summary>
        /// <param name="identity">任务标志</param>
        /// <param name="opener">打开人员</param>
        /// <returns>操作结果</returns>
        public bool UpdateStateAndOpener(Guid identity, Guid opener)
        {
            var updef = BuildUpdateDef(m => m.State, WorkFlowTaskState.Opened);
            updef = BuildUpdateDef(m => m.Opener, opener);
            return Update(identity, updef);
        }
    }
}
